﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MINERP.Statistics.ShopBill
{
    public partial class FrmShopNoPay : Form
    {
        //属性
        private static FrmShopNoPay _instance;
        public static FrmShopNoPay GetInstance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new FrmShopNoPay();
                }
                return _instance;
            }
            set
            {
                _instance = value;
            }
        }
        //参数
        private BLL.Statistics.ShopNoPayBLL bll = new MINERP.BLL.Statistics.ShopNoPayBLL();
        //list
        private List<BLL.Statistics.ShopNoPayBLL.ShopCL> Shops = new List<MINERP.BLL.Statistics.ShopNoPayBLL.ShopCL>();     //商户集合
        private List<int> SelectedShopIds = new List<int>();                                                                //选中的商户集合
        public FrmShopNoPay()
        {
            InitializeComponent();
        }

        private void FrmShopNoPay_Load(object sender, EventArgs e)
        {
            try
            {
                dtStart.DateTime = DateTime.Now;
                dtEnd.DateTime = DateTime.Now;
                GetShop();      //11
                BindArea();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //地区关闭，确定选中的地区
        private void cbArea_Closed(object sender, DevExpress.XtraEditors.Controls.ClosedEventArgs e)
        {
            try
            {
                List<string> areas = new List<string>();
                foreach (DevExpress.XtraEditors.Controls.CheckedListBoxItem item in cbArea.Properties.Items)
                {
                    if (item.CheckState.ToString() == "Checked")
                    {
                        areas.Add(item.ToString());
                    }
                }
                BindShop(areas);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //商户关闭，确定选中的商户
        private void cbShop_Closed(object sender, DevExpress.XtraEditors.Controls.ClosedEventArgs e)
        {
            SelectedShopIds.Clear();
            foreach (DevExpress.XtraEditors.Controls.CheckedListBoxItem item in cbShop.Properties.Items)
            {
                if (item.CheckState.ToString() == "Checked")
                {
                    SelectedShopIds.Add(Convert.ToInt32(item.Value));
                }
            }
        }

        //查询
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (SelectedShopIds.Count == 0)
            {
                MessageBox.Show("未选择统计的商户！", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            try
            {
                Cursor = Cursors.WaitCursor;
                btnOK.Enabled = false;
                Statistic();
                btnOK.Enabled = true;
                Cursor = Cursors.Default;
            }
            catch (Exception ex)
            {

                Cursor = Cursors.Default;
                MessageBox.Show(ex.Message, "确定", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            finally
            {
                btnOK.Enabled = true;
            }
        }
        //-----------公共方法-------------
        private void GetShop()
        {
            try
            {
                Shops = bll.GetShopList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #region 绑定地区
        /// <summary>
        /// 绑定地区 
        /// </summary>
        private void BindArea()
        {
            try
            {
                var areas = Shops.GroupBy(p => p.Area);
                foreach (var area in areas)
                {
                    cbArea.Properties.Items.Add(area.First().Area);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 绑定商户
        /// <summary>
        /// 绑定商户
        /// </summary>
        /// <param name="areas"></param>
        private void BindShop(List<string> areas)
        {
            try
            {
                List<BLL.Statistics.ShopNoPayBLL.ShopCL> selectedShops = new List<MINERP.BLL.Statistics.ShopNoPayBLL.ShopCL>();
                foreach (string area in areas)
                {
                    selectedShops.AddRange(Shops.FindAll(p => p.Area == area));
                }
                cbShop.Properties.DataSource = null;
                cbShop.Properties.DisplayMember = "ShopName";
                cbShop.Properties.ValueMember = "ShopId";
                cbShop.Properties.DataSource = selectedShops;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        /// <summary>
        /// 查询
        /// </summary>
        private void Statistic()
        {
            try
            {
                int noPayMoneyStatus = (int)COM.ShopBillStatus.UnCheckOut;
                //bool dateEnable = true;

                int startYear = 0;
                int endYear = 0;
                int startMonth = 0;
                int endMonth = 0;
                //DateTime start = dtStart.DateTime;
                //DateTime end = dtEnd.DateTime;
                DateTime start = dtStart.DateTime.Date;
                DateTime end = dtEnd.DateTime.AddDays(1).Date.AddSeconds(-1);
                startYear = start.Year;
                startMonth = start.Month;
                endYear = end.Year;
                endMonth = end.Month;
                //DataTable dt = bll.GetShopNoPay(SelectedShopIds, startYear, endYear, startMonth, endMonth, noPayMoneyStatus);
                DataTable dt = bll.GetShopNoPay(SelectedShopIds, start,end, noPayMoneyStatus);
                dt.TableName = "ShopNoPay";

                DataTable dt2 = bll.CreateTimeDt();
                dt2.TableName = "TimeDt";
                string startTime = startYear.ToString() + "-" + startMonth.ToString();
                string endTime = endYear.ToString() + "-" + endMonth.ToString();
                DataRow nr = dt2.NewRow();
                nr["StartTime"] = start;
                nr["EndTime"] = end;
                dt2.Rows.Add(nr);

                DataSet ds = new DataSet();
                ds.Tables.Add(dt);
                ds.Tables.Add(dt2);
                ds.WriteXml(@"C:\WINDOWS\xmlData\shopNoPay.xml", XmlWriteMode.WriteSchema);

                report.Preview = previewControl1;
                report.RegisterData(ds);
                report.Load(Application.StartupPath + "\\shopNoPay.frx");

                report.Show();
                //previewControl1.Show();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }
    }
}
